home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gmp-132.lha / gmp-1.3.2 / mpn_mod_1.c < prev    next >
C/C++ Source or Header  |  1993-05-03  |  3KB  |  105 lines

  1. /* mpn_mod_1(dividend_ptr, dividend_size, divisor_limb) --
  2.    Divide (DIVIDEND_PTR,,DIVIDEND_SIZE) by DIVISOR_LIMB.
  3.    Return the single-limb remainder.
  4.    There are no constraints on the value of the divisor.
  5.  
  6.    QUOT_PTR and DIVIDEND_PTR might point to the same limb.
  7.  
  8. Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  9.  
  10. This file is part of the GNU MP Library.
  11.  
  12. The GNU MP Library is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2, or (at your option)
  15. any later version.
  16.  
  17. The GNU MP Library is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. GNU General Public License for more details.
  21.  
  22. You should have received a copy of the GNU General Public License
  23. along with the GNU MP Library; see the file COPYING.  If not, write to
  24. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  25.  
  26. #include "gmp.h"
  27. #include "gmp-impl.h"
  28. #include "longlong.h"
  29.  
  30. mp_limb
  31. #ifdef __STDC__
  32. mpn_mod_1 (mp_srcptr dividend_ptr, mp_size dividend_size,
  33.         unsigned long int divisor_limb)
  34. #else
  35. mpn_mod_1 (dividend_ptr, dividend_size, divisor_limb)
  36.      mp_srcptr dividend_ptr;
  37.      mp_size dividend_size;
  38.      unsigned long int divisor_limb;
  39. #endif
  40. {
  41.   int normalization_steps;
  42.   mp_size i;
  43.   mp_limb n1, n0, r;
  44.   int dummy;
  45.  
  46.   /* Botch: Should this be handled at all?  Rely on callers?  */
  47.   if (dividend_size == 0)
  48.     return 0;
  49.  
  50.   if (UDIV_NEEDS_NORMALIZATION)
  51.     {
  52.       count_leading_zeros (normalization_steps, divisor_limb);
  53.       if (normalization_steps != 0)
  54.     {
  55.       divisor_limb <<= normalization_steps;
  56.  
  57.       n1 = dividend_ptr[dividend_size - 1];
  58.       r = n1 >> (BITS_PER_MP_LIMB - normalization_steps);
  59.  
  60.       /* Possible optimization:
  61.       if (r == 0
  62.           && divisor_limb > ((n1 << normalization_steps)
  63.                  | (dividend_ptr[dividend_size - 2] >> ...)))
  64.         ...one division less...
  65.        */
  66.  
  67.       for (i = dividend_size - 2; i >= 0; i--)
  68.         {
  69.           n0 = dividend_ptr[i];
  70.           udiv_qrnnd (dummy, r, r,
  71.               ((n1 << normalization_steps)
  72.                | (n0 >> (BITS_PER_MP_LIMB - normalization_steps))),
  73.               divisor_limb);
  74.           n1 = n0;
  75.         }
  76.       udiv_qrnnd (dummy, r, r,
  77.               n1 << normalization_steps,
  78.               divisor_limb);
  79.       return r >> normalization_steps;
  80.     }
  81.     }
  82.  
  83.   /* No normalization needed, either because udiv_qrnnd doesn't require
  84.      it, or because DIVISOR_LIMB is already normalized.  */
  85.  
  86.   i = dividend_size - 1;
  87.   r = dividend_ptr[i];
  88.  
  89.   if (r >= divisor_limb)
  90.     {
  91.       r = 0;
  92.     }
  93.   else
  94.     {
  95.       i--;
  96.     }
  97.  
  98.   for (; i >= 0; i--)
  99.     {
  100.       n0 = dividend_ptr[i];
  101.       udiv_qrnnd (dummy, r, r, n0, divisor_limb);
  102.     }
  103.   return r;
  104. }
  105.